﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using OL.FrameCore.Application.SysLog;
using OL.FrameCore.Application.SysLog.Dto;
using OL.FrameCore.WebUI.ViewModels.SysLog;

namespace OL.FrameCore.WebUI.Controllers
{
    public class SysLogController : ControllerBase
    {
        ISysLogService _sysLogService;

        public SysLogController(ISysLogService sysLogService)
        {
            _sysLogService = sysLogService;
        }

        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取操作日志列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public IActionResult GetUserOperateLogList(GetUserOperateLogListViewRequest request)
        {
            var serviceRequest = ToMap<GetUserOperateLogListViewRequest, GetUserOperateLogListRequest>(request);
            serviceRequest.PageIndex = request.page - 1;
            serviceRequest.PageSize = request.limit;

            var serviceResult = _sysLogService.GetUserOperateLogList(serviceRequest);
            var result = new
            {
                code = 0,
                msg = "",
                count = serviceResult.ItemsCount,
                data = serviceResult.Items
            };

            return Json(result);
        }

        /// <summary>
        /// 获取用户登录日志列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public IActionResult GetUserLoginLogList(GetUserLoginLogListViewRequest request)
        {
            var serviceRequest = ToMap<GetUserLoginLogListViewRequest, GetUserLoginLogListRequest>(request);
            serviceRequest.PageIndex = request.page - 1;
            serviceRequest.PageSize = request.limit;

            var serviceResult = _sysLogService.GetUserLoginLogList(serviceRequest);
            var result = new
            {
                code = 0,
                msg = "",
                count = serviceResult.ItemsCount,
                data = serviceResult.Items
            };

            return Json(result);
        }

        /// <summary>
        /// 获取错误日志列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public IActionResult GetErrorLogList(GetErrorLogListViewRequest request)
        {
            var serviceRequest = ToMap<GetErrorLogListViewRequest, GetErrorLogListRequest>(request);
            serviceRequest.PageIndex = request.page - 1;
            serviceRequest.PageSize = request.limit;

            var serviceResult = _sysLogService.GetErrorLogList(serviceRequest);
            var result = new
            {
                code = 0,
                msg = "",
                count = serviceResult.ItemsCount,
                data = serviceResult.Items
            };

            return Json(result);
        }
    }
}